﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMVC.Areas.AdminPanel.Models;
using BusinessTier.Facade.Interface;
using BusinessTier.Facade.Implementation;
using WebMVC.Helpers;
using WebMVC.Utils;
using Domain;

namespace WebMVC.Areas.AdminPanel.Controllers
{
    public class TemplateController : AdminControllerBase
    {
        private IStaticDataService staticDataService;
        private IProductService productService;

        public TemplateController()
            : this(new StaticDataService(), new ProductService())
        {}

        public TemplateController(IStaticDataService staticDataService, IProductService productService)
        {
            this.staticDataService = staticDataService;
            this.productService = productService;
        }

        public ActionResult Index()
        {/*
            var model = new List<TemplateViewModel>();
            var templates = productService.GetAllTemplates(null);
            foreach (var template in templates)
            {
                model.Add(new TemplateViewModel() { 
                    Id = template.Id,
                    CreateDate = template.CreateDate,
                    NameAm = template.NameAm,
                    NameRu = template.NameRu,
                    NameEn = template.NameEn,
                });
            }
            return View(model);/*
          * */
            return View();
        }

        //
        // GET: /AdminPanel/Template/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /AdminPanel/Template/Create

        public ActionResult Create()
        {
            var model = new TemplateCreateViewModel()
            {
                Fonts = GetAllFonts()
            };
            return View(model);
        } 

        //
        // POST: /AdminPanel/Template/Create

        [HttpPost]
        public ActionResult Create(TemplateCreateViewModel model)
        {
            /*
            var dataTemplateObj = new DataTemplateDO() { 
                Template = (new XMLSerializer()).DeserializeInfoTemplateJSON(model.TemplateJSON),
                NameAm = model.NameAm,
                NameEn = model.NameEn,
                NameRu = model.NameRu
            };
            int id = productService.CreateTemplate(dataTemplateObj);
            
            this.Info = "New template with ID[" + id + "] was successfully created";*/
            return RedirectToAction("Index");
        }
        
        //
        // GET: /AdminPanel/Template/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /AdminPanel/Template/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminPanel/Template/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /AdminPanel/Template/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        [Authorize]
        [HttpPost]
        public ActionResult HandleBackgroundImg()
        {
            string uploadDir = Server.MapPath("/Img/temp/product/");

            if (this.Request.Files.Count == 0)
            {
                return new EmptyResult();
            }

            HttpPostedFileBase file = Request.Files[0];
            if (ImageUtils.IsNormalJpeg(file.InputStream))
            {
                //Clear previous temp
                FileStoreHelper.DeleteProductTempFile(uploadDir, CurrentUserId.Value);

                string fileName = FileStoreHelper.SaveProductTempImage(uploadDir, CurrentUserId.Value, file);

                return Json(new { Valid = true, FilePath = "/Img/temp/product/" + fileName + "?" + DateTime.Now.ToBinary() }, "text/plain");
            }
            return Json(new { Valid = false, FilePath = "" }, "text/plain");

        }

        private List<SelectListItem> GetAllFonts()
        {
            var fonts = staticDataService.GetAllFonts();

            var list = new List<SelectListItem>();
            foreach (var font in fonts)
            {
                list.Add(new SelectListItem() { 
                    Text = font.Name + " " + font.Style,
                    Value = font.FontId.ToString()
                });
            }
            return list;
        }
    }
}
